/**
 * Created by ASUS on 2017/11/30.
 */


    //测试
    var filterObjValue;

    //阻尼系数
    var deceleration = mui.os.ios?0.003:0.0009;
    mui('.mui-scroll-wrapper').scroll({
        bounce: false,
        indicators: true, //是否显示滚动条
        deceleration:deceleration
    });

    Vue.filter("toDate",function(value){
        return formatDate(new Date(value),"yyyy-MM-dd");
    });
    //初始化分页计数数组   !重要
    var paginationArr = [1];
    //生成多个该页面对应列表容器的vue对象 , 根据分页数组循环该数组来控制vue列表容器对象 应该生成多少个,并且用数组存储 !重要
    var vueObjArr = [];

    mui.ready(function() {

        for(var i=0;i<($(".mui-control-item").length-1);i++){
            paginationArr.push(1);
        };


        for(var i =0;i<paginationArr.length;i++){
            // 初始化vue对象
            vueObjArr.push(new Vue({
                el: '#vueListBox'+i,
                data:{
                    boxName:'#vueListBox'+i,
                    datas:[]
                },
                directives: {
                    focus: {
                        // 指令的定义
                        inserted: function (el) {
                            initFreeSwiper({
                                target:"."+$(el).attr("selfCode"),
                                clickTar:$("."+$(el).find(".swiper-slide").attr("selfCode"))
                            });
                        }
                    }
                }
            }));
        }
        //默认开始请求第一页的数据
        requestData({
            tabNum:0,
            success:function(msg) {
                    if(msg.obj==null||msg.obj==undefined){
                        return;
                    }
                    msg.obj.map(function(ele,index){
                        vueObjArr[0].datas.push(ele);
                    });
                    //数据加完之后删除loading动画
                    $(vueObjArr[0].boxName).find(".mui-loading").remove();
            }
        });
        //循环初始化所有下拉刷新，上拉加载。
        mui.each(document.querySelectorAll('.mui-scroll'), function(index, pullRefreshEl) {

            if($(pullRefreshEl).hasClass("aside-scroll-content")){
                console.log(pullRefreshEl);
                return;
            }

            mui(pullRefreshEl).pullToRefresh({
                down: {

                    callback: function() {
                        var self = this;
                        var ul = self.element.querySelector('.mui-table-view');
                        self.endPullDownToRefresh();
                        // console.log("下拉刷新")
                        // if($(".hospitalInfoBox").length){
                        //     $(".hospitalInfoBox").hide();
                        // }
                    }
                },
                up: {
                    style:'circle',
                    contentnomore:'没有更多数据了',
                    callback: function() {
                        var self = this;
                        if(self.element.querySelector('.mui-table-view')==null){return;}
                        var ul = self.element.querySelector('.mui-table-view');
                        var indexNum = $(ul).parents(".mui-slider-item").index();

                        if(indexNum==-1){
                            indexNum = 0;
                        }
                        requestData({
                            tabNum:indexNum,
                            success:function(msg) {
                                if(msg.obj==null||msg.obj==undefined){
                                    self.endPullUpToRefresh(!msg.more);
                                    return;
                                }
                                    msg.obj.map(function(ele,index){
                                        vueObjArr[indexNum].datas.push(ele);
                                    });
                                    //数据加完之后删除loading动画
                                    $(vueObjArr[indexNum].boxName).find(".mui-loading").remove();
                                    self.endPullUpToRefresh(!msg.more);
                            }
                        });
                    }
                }
            });
        });
        if(paginationArr.length>1){
            document.getElementById('slider').addEventListener('slide', function(e) {
                var indexNum = e.detail.slideNumber;
                if($(vueObjArr[indexNum].boxName).find(".mui-loading").length){
                    requestData({
                        tabNum:indexNum,
                        success:function(msg) {
                                if(msg.obj==null||msg.obj==undefined){
                                    return;
                                }
                                msg.obj.map(function(ele,index){
                                    vueObjArr[indexNum].datas.push(ele);
                                });
                                //数据加完之后删除loading动画
                                $(vueObjArr[indexNum].boxName).find(".mui-loading").remove();
                        }
                    });
                }
            });
        }
    });

    function requestData(obj){
        var defaultObj = {
            tabNum:0,
            success:null
        };
        for(var i in defaultObj){
            defaultObj[i] = obj[i];
        }
        //这里获取数据需要两个必要的参数,一个是当前的tab的下标,一个是第几页
        var dataType = defaultObj.tabNum+1;
        var currentPage = paginationArr[defaultObj.tabNum];
        var url = "";
        if(filterObjValue!=undefined){
            var str = "";
            for(var i in filterObjValue){
                if(i == "type"||i=="orderWay"){
                    continue;
                }
                str += ("&"+i+"=");
                if(i=="areaId"){
                    if(filterObjValue[i]["areaId"]==undefined){

                    }else{
                        str += (filterObjValue[i]["areaId"]+",")
                    }

                }else{
                    for(var y in filterObjValue[i]){
                        str += (filterObjValue[i][y] + ",")
                    }
                }
                str = str.substring(0,str.length-1);
            }

            var listType = "0";
            var orderWay = 0;
            if(filterObjValue.type!=undefined){
                listType = filterObjValue.type;
            }
            if(filterObjValue.orderWay!=undefined){
                orderWay = filterObjValue.orderWay;
            }
            url = dataListUrl+"dataType="+dataType+"&page="+currentPage+"&type="+listType+"&orderWay="+orderWay+str;
        }else{
            url = dataListUrl+"dataType="+dataType+"&page="+currentPage;
        }


        $.get(url,function(msg){
            //数据拿到之后需要根据tab下标来改变数组对应存储页码的值
            paginationArr[defaultObj.tabNum] = paginationArr[defaultObj.tabNum]+1;
            //数据拿到之后将数据给success方法执行
            defaultObj.success(msg);

        })
    }

